<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 6.3.0">
<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic,400,400italic,700,700italic%7CZCOOL+QingKe+HuangYou:300,300italic,400,400italic,700,700italic%7CNoto+Sans+Simplified+Chinese:300,300italic,400,400italic,700,700italic%7CJetBrains+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="/lib/@fortawesome/fontawesome-free/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/animate.css/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="/lib/@fancyapps/ui/dist/fancybox/fancybox.css" integrity="sha256-RvRHGSuWAxZpXKV9lLDt2e+rZ+btzn48Wp4ueS3NZKs=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"wyatex.online","root":"/","images":"/images","scheme":"Pisces","darkmode":true,"version":"8.18.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":true}}</script><script src="/js/config.js"></script>

    <meta name="description" content="欢迎来到第二天，现在我们已经看到了相当数量的 Rust 代码，我们将继续：  结构、枚举、方法。 模式匹配：解构枚举、结构和数组。 控制流构造：if, if let, while, while let, break, 和 continue. 标准库： String, Option 和 Result, Vec, HashMap, Rc 和 Arc. 模块：可见性、路径和文件系统层次结构。">
<meta property="og:type" content="article">
<meta property="og:title" content="rust学习笔记（3）：Day2 Moring">
<meta property="og:url" content="https://wyatex.online/Rust/rust-note-3-google/index.html">
<meta property="og:site_name" content="Wyatex&#96;s blog">
<meta property="og:description" content="欢迎来到第二天，现在我们已经看到了相当数量的 Rust 代码，我们将继续：  结构、枚举、方法。 模式匹配：解构枚举、结构和数组。 控制流构造：if, if let, while, while let, break, 和 continue. 标准库： String, Option 和 Result, Vec, HashMap, Rc 和 Arc. 模块：可见性、路径和文件系统层次结构。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-02-05T02:24:50.000Z">
<meta property="article:modified_time" content="2023-05-05T01:25:47.239Z">
<meta property="article:author" content="Wyatex">
<meta property="article:tag" content="学习笔记">
<meta property="article:tag" content="Rust">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://wyatex.online/Rust/rust-note-3-google/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://wyatex.online/Rust/rust-note-3-google/","path":"Rust/rust-note-3-google/","title":"rust学习笔记（3）：Day2 Moring"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>rust学习笔记（3）：Day2 Moring | Wyatex`s blog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Wyatex`s blog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">97</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">26</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">155</span></a></li><li class="menu-item menu-item-编程"><a href="/programming/" rel="section"><i class="fa fa-book fa-fw"></i>编程</a></li><li class="menu-item menu-item-蹲坑读物"><a href="/%E5%B9%B2%E8%B4%A7/%E5%BC%80%E5%8F%91%E4%BA%A4%E6%B5%81%E7%BE%A4%E5%88%86%E4%BA%AB%E8%AE%B0%E5%BD%95/" rel="section"><i class="fa fa-book fa-fw"></i>蹲坑读物</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93"><span class="nav-number">1.</span> <span class="nav-text">结构体</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%83%E7%BB%84%E7%BB%93%E6%9E%84%E4%BD%93-Tuple-Structs"><span class="nav-number">2.</span> <span class="nav-text">元组结构体 &#x2F; Tuple Structs</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%97%E6%AE%B5%E7%AE%80%E5%86%99%E8%AF%AD%E6%B3%95"><span class="nav-number">3.</span> <span class="nav-text">字段简写语法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9E%9A%E4%B8%BE"><span class="nav-number">4.</span> <span class="nav-text">枚举</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BD%BD%E8%8D%B7%E5%8F%98%E4%BD%93"><span class="nav-number">4.1.</span> <span class="nav-text">载荷变体</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9E%9A%E4%B8%BE%E5%A4%A7%E5%B0%8F"><span class="nav-number">4.2.</span> <span class="nav-text">枚举大小</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%96%B9%E6%B3%95"><span class="nav-number">5.</span> <span class="nav-text">方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%B9%E6%B3%95%E6%8E%A5%E6%94%B6%E8%80%85"><span class="nav-number">5.1.</span> <span class="nav-text">方法接收者</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BE%8B%E5%AD%90"><span class="nav-number">5.2.</span> <span class="nav-text">例子</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D-pattern-matching"><span class="nav-number">6.</span> <span class="nav-text">模式匹配 &#x2F; pattern matching</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A7%A3%E6%9E%84%E6%9E%9A%E4%B8%BE"><span class="nav-number">6.1.</span> <span class="nav-text">解构枚举</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A7%A3%E6%9E%84%E7%BB%93%E6%9E%84%E4%BD%93"><span class="nav-number">6.2.</span> <span class="nav-text">解构结构体</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A7%A3%E6%9E%84%E6%95%B0%E7%BB%84"><span class="nav-number">6.3.</span> <span class="nav-text">解构数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8C%B9%E9%85%8D%E5%AE%88%E5%8D%AB"><span class="nav-number">6.4.</span> <span class="nav-text">匹配守卫</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%83%E4%B9%A0%E9%A2%98"><span class="nav-number">7.</span> <span class="nav-text">练习题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%81%A5%E5%BA%B7%E7%BB%9F%E8%AE%A1"><span class="nav-number">7.1.</span> <span class="nav-text">健康统计</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E8%BE%B9%E5%BD%A2%E7%BB%93%E6%9E%84"><span class="nav-number">7.2.</span> <span class="nav-text">多边形结构</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Wyatex"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Wyatex</p>
  <div class="site-description" itemprop="description">这里是我用来做笔记的网站哟~</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">155</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">26</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">97</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wyatex" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wyatex" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:wyatex@qq.com" title="E-Mail → mailto:wyatex@qq.com" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
    <div class="sidebar-inner sidebar-blogroll">
      <div class="links-of-blogroll animated">
        <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
          链接
        </div>
        <ul class="links-of-blogroll-list">
            <li class="links-of-blogroll-item">
              <a href="https://antfu.me/" title="https:&#x2F;&#x2F;antfu.me&#x2F;" rel="noopener" target="_blank">Anthony Fu</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://www.codesky.me/" title="https:&#x2F;&#x2F;www.codesky.me&#x2F;" rel="noopener" target="_blank">CodeSky</a>
            </li>
            <li class="links-of-blogroll-item">
              <a href="https://learn.lianglianglee.com/" title="https:&#x2F;&#x2F;learn.lianglianglee.com&#x2F;" rel="noopener" target="_blank">技术文章摘抄</a>
            </li>
        </ul>
      </div>
    </div>
        <div class="pjax">
        <div class="sidebar-inner sidebar-post-related">
          <div class="animated">
              <div class="links-of-blogroll-title"><i class="fa fa-signs-post fa-fw"></i>
    相关文章
  </div>
  <ul class="popular-posts">
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/uncategorized/JsonSchema%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="bookmark">
        <time class="popular-posts-time">2021-10-19</time>
        <br>
      JsonSchema学习笔记
      </a>
    </li>
    <li class="popular-posts-item">
      <a class="popular-posts-link" href="/Rust/rust-note-course-1/" rel="bookmark">
        <time class="popular-posts-time">2024-01-03</time>
        <br>
      Rust圣经学习笔记（1）
      </a>
    </li>
  </ul>

          </div>
        </div>
        </div>
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wyatex.online/Rust/rust-note-3-google/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Wyatex">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Wyatex`s blog">
      <meta itemprop="description" content="这里是我用来做笔记的网站哟~">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="rust学习笔记（3）：Day2 Moring | Wyatex`s blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          rust学习笔记（3）：Day2 Moring
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-02-05 10:24:50" itemprop="dateCreated datePublished" datetime="2023-02-05T10:24:50+08:00">2023-02-05</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-05-05 09:25:47" itemprop="dateModified" datetime="2023-05-05T09:25:47+08:00">2023-05-05</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Rust/" itemprop="url" rel="index"><span itemprop="name">Rust</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>欢迎来到第二天，现在我们已经看到了相当数量的 Rust 代码，我们将继续：</p>
<ul>
<li>结构、枚举、方法。</li>
<li>模式匹配：解构枚举、结构和数组。</li>
<li>控制流构造：<code>if</code>, <code>if let</code>, <code>while</code>, <code>while let</code>, <code>break</code>, 和 <code>continue</code>.</li>
<li>标准库： <code>String</code>, <code>Option</code> 和 <code>Result</code>, <code>Vec</code>, <code>HashMap</code>, <code>Rc</code> 和 <code>Arc</code>.</li>
<li>模块：可见性、路径和文件系统层次结构。</li>
</ul>
<span id="more"></span>

<h2 id="结构体"><a href="#结构体" class="headerlink" title="结构体"></a>结构体</h2><p>类似 C 、 C++：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">    name: <span class="type">String</span>,</span><br><span class="line">    age: <span class="type">u8</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">peter</span> = Person &#123;</span><br><span class="line">        name: <span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;Peter&quot;</span>),</span><br><span class="line">        age: <span class="number">27</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;&#123;&#125; is &#123;&#125; years old&quot;</span>, peter.name, peter.age);</span><br><span class="line">    </span><br><span class="line">    peter.age = <span class="number">28</span>;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;&#123;&#125; is &#123;&#125; years old&quot;</span>, peter.name, peter.age);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">let</span> <span class="variable">jackie</span> = Person &#123;</span><br><span class="line">        name: <span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;Jackie&quot;</span>),</span><br><span class="line">        ..peter</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;&#123;&#125; is &#123;&#125; years old&quot;</span>, jackie.name, jackie.age);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="元组结构体-Tuple-Structs"><a href="#元组结构体-Tuple-Structs" class="headerlink" title="元组结构体 &#x2F; Tuple Structs"></a>元组结构体 &#x2F; Tuple Structs</h2><p>如果字段名不重要，可以使用元组结构体:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Point</span>(<span class="type">i32</span>, <span class="type">i32</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">p</span> = <span class="title function_ invoke__">Point</span>(<span class="number">17</span>, <span class="number">23</span>);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;(&#123;&#125;, &#123;&#125;)&quot;</span>, p.<span class="number">0</span>, p.<span class="number">1</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这通常用于单字段包装器(称为newtypes):</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">PoundOfForce</span>(<span class="type">f64</span>);</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Newtons</span>(<span class="type">f64</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">compute_thruster_force</span>() <span class="punctuation">-&gt;</span> PoundOfForce &#123;</span><br><span class="line">    todo!(<span class="string">&quot;Ask a rocket scientist at NASA&quot;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">set_thruster_force</span>(force: Newtons) &#123;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">force</span> = <span class="title function_ invoke__">compute_thruster_force</span>();</span><br><span class="line">    <span class="title function_ invoke__">set_thruster_force</span>(force); <span class="comment">// 报错，类型不一样</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="字段简写语法"><a href="#字段简写语法" class="headerlink" title="字段简写语法"></a>字段简写语法</h2><p>如果已经拥有具有正确名称的变量，则可以使用简写创建结构体：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#[derive(Debug)]</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">    name: <span class="type">String</span>,</span><br><span class="line">    age: <span class="type">u8</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">new</span>(name: <span class="type">String</span>, age: <span class="type">u8</span>) <span class="punctuation">-&gt;</span> Person &#123;</span><br><span class="line">        Person &#123; name, age &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">peter</span> = Person::<span class="title function_ invoke__">new</span>(<span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;Peter&quot;</span>), <span class="number">27</span>);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;&#123;peter:?&#125;&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="枚举"><a href="#枚举" class="headerlink" title="枚举"></a>枚举</h2><p><code>enum</code>关键字允许创建具有几个不同变体的类型:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">generate_random_number</span>() <span class="punctuation">-&gt;</span> <span class="type">i32</span> &#123;</span><br><span class="line">    <span class="number">4</span>  <span class="comment">// Chosen by fair dice roll. Guaranteed to be random.</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#[derive(Debug)]</span></span><br><span class="line"><span class="keyword">enum</span> <span class="title class_">CoinFlip</span> &#123;</span><br><span class="line">    Heads,</span><br><span class="line">    Tails,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">flip_coin</span>() <span class="punctuation">-&gt;</span> CoinFlip &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">random_number</span> = <span class="title function_ invoke__">generate_random_number</span>();</span><br><span class="line">    <span class="keyword">if</span> random_number % <span class="number">2</span> == <span class="number">0</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> CoinFlip::Heads;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> CoinFlip::Tails;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;You got: &#123;:?&#125;&quot;</span>, <span class="title function_ invoke__">flip_coin</span>());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="载荷变体"><a href="#载荷变体" class="headerlink" title="载荷变体"></a>载荷变体</h3><p>您可以定义更丰富的枚举，其中变量携带数据。然后可以使用match语句从每个变量中提取数据：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> <span class="title class_">WebEvent</span> &#123;</span><br><span class="line">    PageLoad,                 <span class="comment">// Variant without payload</span></span><br><span class="line">    <span class="title function_ invoke__">KeyPress</span>(<span class="type">char</span>),           <span class="comment">// Tuple struct variant</span></span><br><span class="line">    Click &#123; x: <span class="type">i64</span>, y: <span class="type">i64</span> &#125;, <span class="comment">// Full struct variant</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#[rustfmt::skip]</span></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">inspect</span>(event: WebEvent) &#123;</span><br><span class="line">    <span class="keyword">match</span> event &#123;</span><br><span class="line">        WebEvent::PageLoad       =&gt; <span class="built_in">println!</span>(<span class="string">&quot;page loaded&quot;</span>),</span><br><span class="line">        WebEvent::<span class="title function_ invoke__">KeyPress</span>(c)    =&gt; <span class="built_in">println!</span>(<span class="string">&quot;pressed &#x27;&#123;c&#125;&#x27;&quot;</span>),</span><br><span class="line">        WebEvent::Click &#123; x, y &#125; =&gt; <span class="built_in">println!</span>(<span class="string">&quot;clicked at x=&#123;x&#125;, y=&#123;y&#125;&quot;</span>),</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">load</span> = WebEvent::PageLoad;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">press</span> = WebEvent::<span class="title function_ invoke__">KeyPress</span>(<span class="string">&#x27;x&#x27;</span>);</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">click</span> = WebEvent::Click &#123; x: <span class="number">20</span>, y: <span class="number">80</span> &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="title function_ invoke__">inspect</span>(load);</span><br><span class="line">    <span class="title function_ invoke__">inspect</span>(press);</span><br><span class="line">    <span class="title function_ invoke__">inspect</span>(click);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="枚举大小"><a href="#枚举大小" class="headerlink" title="枚举大小"></a>枚举大小</h3><p>Rust枚举被紧密地打包，考虑到由于对齐的限制:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">use</span> std::mem::&#123;align_of, size_of&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">macro_rules!</span> dbg_size &#123;</span><br><span class="line">    ($t:ty) =&gt; &#123;</span><br><span class="line">        <span class="built_in">println!</span>(<span class="string">&quot;&#123;&#125;: size &#123;&#125; bytes, align: &#123;&#125; bytes&quot;</span>,</span><br><span class="line">                 <span class="built_in">stringify!</span>($t), size_of::&lt;$t&gt;(), align_of::&lt;$t&gt;());</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">enum</span> <span class="title class_">Foo</span> &#123;</span><br><span class="line">    A,</span><br><span class="line">    B,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#[repr(u32)]</span></span><br><span class="line"><span class="keyword">enum</span> <span class="title class_">Bar</span> &#123;</span><br><span class="line">    A,  <span class="comment">// 0</span></span><br><span class="line">    B = <span class="number">10000</span>,</span><br><span class="line">    C,  <span class="comment">// 10001</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    dbg_size!(Foo);</span><br><span class="line">    dbg_size!(Bar);</span><br><span class="line">    dbg_size!(<span class="type">bool</span>);</span><br><span class="line">    dbg_size!(<span class="type">Option</span>&lt;<span class="type">bool</span>&gt;);</span><br><span class="line">    dbg_size!(&amp;<span class="type">i32</span>);</span><br><span class="line">    dbg_size!(<span class="type">Option</span>&lt;&amp;<span class="type">i32</span>&gt;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h2><p>Rust允许您将函数与新类型关联起来，使用<code>impl</code>语句:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#[derive(Debug)]</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">    name: <span class="type">String</span>,</span><br><span class="line">    age: <span class="type">u8</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">Person</span> &#123;</span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">say_hello</span>(&amp;<span class="keyword">self</span>) &#123;</span><br><span class="line">        <span class="built_in">println!</span>(<span class="string">&quot;Hello, my name is &#123;&#125;&quot;</span>, <span class="keyword">self</span>.name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">peter</span> = Person &#123;</span><br><span class="line">        name: <span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;Peter&quot;</span>),</span><br><span class="line">        age: <span class="number">27</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">    peter.<span class="title function_ invoke__">say_hello</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="方法接收者"><a href="#方法接收者" class="headerlink" title="方法接收者"></a>方法接收者</h3><p>上面的<code>&amp;self</code>表示该方法不可变地借用对象。方法还有其他可能的接收者:</p>
<ul>
<li><code>&amp;self</code>:使用共享的和不可变的引用从调用者那里借用对象。对象之后可以再次使用。</li>
<li><code>&amp;mut self</code>:使用唯一且可变的引用从调用者那里借用对象。对象之后可以再次使用。</li>
<li><code>self</code>: 获取对象的所有权并将其移离调用者。方法成为对象的所有者。当方法返回时，该对象将被删除(释放)，除非显式传输其所有权。</li>
<li><code>mut self</code>:与上面相同，但是当方法拥有对象时，它也可以改变它。完全的所有权并不自动意味着可变性。</li>
<li>没有接收者:这将成为结构上的静态方法。通常用于创建按照约定称为new的构造函数。</li>
</ul>
<p>除了<code>self</code>上的变量之外，还有一些特殊的包装器类型允许作为接收器类型，例如<code>Box&lt;self&gt;</code>。</p>
<h3 id="例子"><a href="#例子" class="headerlink" title="例子"></a>例子</h3><figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#[derive(Debug)]</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Race</span> &#123;</span><br><span class="line">    name: <span class="type">String</span>,</span><br><span class="line">    laps: <span class="type">Vec</span>&lt;<span class="type">i32</span>&gt;,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">Race</span> &#123;</span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">new</span>(name: &amp;<span class="type">str</span>) <span class="punctuation">-&gt;</span> Race &#123;  <span class="comment">// 没有接收者, 一个静态方法</span></span><br><span class="line">        Race &#123; name: <span class="type">String</span>::<span class="title function_ invoke__">from</span>(name), laps: <span class="type">Vec</span>::<span class="title function_ invoke__">new</span>() &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">add_lap</span>(&amp;<span class="keyword">mut</span> <span class="keyword">self</span>, lap: <span class="type">i32</span>) &#123;  <span class="comment">// 借用调用者而且允许修改，后面调用者可继续使用</span></span><br><span class="line">        <span class="keyword">self</span>.laps.<span class="title function_ invoke__">push</span>(lap);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">print_laps</span>(&amp;<span class="keyword">self</span>) &#123;  <span class="comment">// 借用而且只获取读权限给self</span></span><br><span class="line">        <span class="built_in">println!</span>(<span class="string">&quot;Recorded &#123;&#125; laps for &#123;&#125;:&quot;</span>, <span class="keyword">self</span>.laps.<span class="title function_ invoke__">len</span>(), <span class="keyword">self</span>.name);</span><br><span class="line">        <span class="keyword">for</span> (idx, lap) <span class="keyword">in</span> <span class="keyword">self</span>.laps.<span class="title function_ invoke__">iter</span>().<span class="title function_ invoke__">enumerate</span>() &#123;</span><br><span class="line">            <span class="built_in">println!</span>(<span class="string">&quot;Lap &#123;idx&#125;: &#123;lap&#125; sec&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">finish</span>(<span class="keyword">self</span>) &#123;  <span class="comment">// 独占所有权</span></span><br><span class="line">        <span class="keyword">let</span> <span class="variable">total</span> = <span class="keyword">self</span>.laps.<span class="title function_ invoke__">iter</span>().sum::&lt;<span class="type">i32</span>&gt;();</span><br><span class="line">        <span class="built_in">println!</span>(<span class="string">&quot;Race &#123;&#125; is finished, total lap time: &#123;&#125;&quot;</span>, <span class="keyword">self</span>.name, total);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">race</span> = Race::<span class="title function_ invoke__">new</span>(<span class="string">&quot;Monaco Grand Prix&quot;</span>);</span><br><span class="line">    race.<span class="title function_ invoke__">add_lap</span>(<span class="number">70</span>);</span><br><span class="line">    race.<span class="title function_ invoke__">add_lap</span>(<span class="number">68</span>);</span><br><span class="line">    race.<span class="title function_ invoke__">print_laps</span>();</span><br><span class="line">    race.<span class="title function_ invoke__">add_lap</span>(<span class="number">71</span>);</span><br><span class="line">    race.<span class="title function_ invoke__">print_laps</span>();</span><br><span class="line">    race.<span class="title function_ invoke__">finish</span>();</span><br><span class="line">    <span class="comment">// race.add_lap(42); // 这里不能再使用，所有权已经被转移</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="模式匹配-pattern-matching"><a href="#模式匹配-pattern-matching" class="headerlink" title="模式匹配 &#x2F; pattern matching"></a>模式匹配 &#x2F; pattern matching</h2><p><code>match</code>关键字允许您根据一个或多个模式匹配一个值。从上到下进行比较，第一个匹配的胜出。</p>
<p>模式可以是简单的值，类似于C和C++中的<code>switch</code>:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">input</span> = <span class="string">&#x27;x&#x27;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">match</span> input &#123;</span><br><span class="line">        <span class="string">&#x27;q&#x27;</span>                   =&gt; <span class="built_in">println!</span>(<span class="string">&quot;Quitting&quot;</span>),</span><br><span class="line">        <span class="string">&#x27;a&#x27;</span> | <span class="string">&#x27;s&#x27;</span> | <span class="string">&#x27;w&#x27;</span> | <span class="string">&#x27;d&#x27;</span> =&gt; <span class="built_in">println!</span>(<span class="string">&quot;Moving around&quot;</span>),</span><br><span class="line">        <span class="string">&#x27;0&#x27;</span>..=<span class="string">&#x27;9&#x27;</span>             =&gt; <span class="built_in">println!</span>(<span class="string">&quot;Number input&quot;</span>),</span><br><span class="line">        _                     =&gt; <span class="built_in">println!</span>(<span class="string">&quot;Something else&quot;</span>),</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>_</code> 模式是匹配任何值的通配符模式。</p>
<h3 id="解构枚举"><a href="#解构枚举" class="headerlink" title="解构枚举"></a>解构枚举</h3><p>模式还可以用于将变量绑定到值的一部分。这是检查类型结构的方法。让我们从一个简单的枚举类型开始:</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> <span class="title class_">Result</span> &#123;</span><br><span class="line">    <span class="title function_ invoke__">Ok</span>(<span class="type">i32</span>),</span><br><span class="line">    <span class="title function_ invoke__">Err</span>(<span class="type">String</span>),</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">divide_in_two</span>(n: <span class="type">i32</span>) <span class="punctuation">-&gt;</span> <span class="type">Result</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> n % <span class="number">2</span> == <span class="number">0</span> &#123;</span><br><span class="line">        <span class="type">Result</span>::<span class="title function_ invoke__">Ok</span>(n / <span class="number">2</span>)</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="type">Result</span>::<span class="title function_ invoke__">Err</span>(<span class="built_in">format!</span>(<span class="string">&quot;cannot divide &#123;n&#125; into two equal parts&quot;</span>))</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">n</span> = <span class="number">100</span>;</span><br><span class="line">    <span class="keyword">match</span> <span class="title function_ invoke__">divide_in_two</span>(n) &#123;</span><br><span class="line">        <span class="type">Result</span>::<span class="title function_ invoke__">Ok</span>(half) =&gt; <span class="built_in">println!</span>(<span class="string">&quot;&#123;n&#125; divided in two is &#123;half&#125;&quot;</span>),</span><br><span class="line">        <span class="type">Result</span>::<span class="title function_ invoke__">Err</span>(msg) =&gt; <span class="built_in">println!</span>(<span class="string">&quot;sorry, an error happened: &#123;msg&#125;&quot;</span>),</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>这里的解构和ES6的解构非常像，这里如果Result如果是Ok则将Ok里的i32类型的值提取出来放到half。</p>
</blockquote>
<h3 id="解构结构体"><a href="#解构结构体" class="headerlink" title="解构结构体"></a>解构结构体</h3><p>同样可以对结构体进行解构</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">Foo</span> &#123;</span><br><span class="line">    x: (<span class="type">u32</span>, <span class="type">u32</span>),</span><br><span class="line">    y: <span class="type">u32</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#[rustfmt::skip]</span></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">foo</span> = Foo &#123; x: (<span class="number">1</span>, <span class="number">2</span>), y: <span class="number">3</span> &#125;;</span><br><span class="line">    <span class="keyword">match</span> foo &#123;</span><br><span class="line">        Foo &#123; x: (<span class="number">1</span>, b), y &#125; =&gt; <span class="built_in">println!</span>(<span class="string">&quot;x.0 = 1, b = &#123;b&#125;, y = &#123;y&#125;&quot;</span>),</span><br><span class="line">        Foo &#123; y: <span class="number">2</span>, x: i &#125;   =&gt; <span class="built_in">println!</span>(<span class="string">&quot;y = 2, i = &#123;i:?&#125;&quot;</span>),</span><br><span class="line">        Foo &#123; y, .. &#125;        =&gt; <span class="built_in">println!</span>(<span class="string">&quot;y = &#123;y&#125;, other fields were ignored&quot;</span>),</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="解构数组"><a href="#解构数组" class="headerlink" title="解构数组"></a>解构数组</h3><p>可以通过匹配数组、元组和切片的元素来解构数组、元组和切片</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#[rustfmt::skip]</span></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">triple</span> = [<span class="number">0</span>, -<span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;Tell me about &#123;triple:?&#125;&quot;</span>);</span><br><span class="line">    <span class="keyword">match</span> triple &#123;</span><br><span class="line">        [<span class="number">0</span>, y, z] =&gt; <span class="built_in">println!</span>(<span class="string">&quot;First is 0, y = &#123;y&#125;, and z = &#123;z&#125;&quot;</span>),</span><br><span class="line">        [<span class="number">1</span>, ..]   =&gt; <span class="built_in">println!</span>(<span class="string">&quot;First is 1 and the rest were ignored&quot;</span>),</span><br><span class="line">        _         =&gt; <span class="built_in">println!</span>(<span class="string">&quot;All elements were ignored&quot;</span>),</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="匹配守卫"><a href="#匹配守卫" class="headerlink" title="匹配守卫"></a>匹配守卫</h3><p>在匹配时，您可以向模式添加保护。这是一个任意的布尔表达式，如果模式匹配就会执行：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#[rustfmt::skip]</span></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">pair</span> = (<span class="number">2</span>, -<span class="number">2</span>);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;Tell me about &#123;pair:?&#125;&quot;</span>);</span><br><span class="line">    <span class="keyword">match</span> pair &#123;</span><br><span class="line">        (x, y) <span class="keyword">if</span> x == y     =&gt; <span class="built_in">println!</span>(<span class="string">&quot;These are twins&quot;</span>),</span><br><span class="line">        (x, y) <span class="keyword">if</span> x + y == <span class="number">0</span> =&gt; <span class="built_in">println!</span>(<span class="string">&quot;Antimatter, kaboom!&quot;</span>),</span><br><span class="line">        (x, _) <span class="keyword">if</span> x % <span class="number">2</span> == <span class="number">1</span> =&gt; <span class="built_in">println!</span>(<span class="string">&quot;The first one is odd&quot;</span>),</span><br><span class="line">        _                    =&gt; <span class="built_in">println!</span>(<span class="string">&quot;No correlation...&quot;</span>),</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="练习题"><a href="#练习题" class="headerlink" title="练习题"></a>练习题</h2><p>我们将在两种上下文中讨论方法的实现：</p>
<ul>
<li>跟踪健康统计数据的简单结构。</li>
<li>绘图库的多结构体和枚举</li>
</ul>
<h3 id="健康统计"><a href="#健康统计" class="headerlink" title="健康统计"></a>健康统计</h3><p>您正在实施一个健康监测系统。作为其中的一部分，需要跟踪用户的健康统计信息。</p>
<p>您将从impl块中的一些存根函数和User结构定义开始。您的目标是在impl块中定义的User结构上实现存根方法。</p>
<p>补上缺少的代码：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// <span class="doctag">TODO:</span> remove this when you&#x27;re done with your implementation.</span></span><br><span class="line"><span class="meta">#![allow(unused_variables, dead_code)]</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">User</span> &#123;</span><br><span class="line">    name: <span class="type">String</span>,</span><br><span class="line">    age: <span class="type">u32</span>,</span><br><span class="line">    weight: <span class="type">f32</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">User</span> &#123;</span><br><span class="line">    <span class="keyword">pub</span> <span class="keyword">fn</span> <span class="title function_">new</span>(name: <span class="type">String</span>, age: <span class="type">u32</span>, weight: <span class="type">f32</span>) <span class="punctuation">-&gt;</span> <span class="keyword">Self</span> &#123;</span><br><span class="line">        <span class="built_in">unimplemented!</span>()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">pub</span> <span class="keyword">fn</span> <span class="title function_">name</span>(&amp;<span class="keyword">self</span>) <span class="punctuation">-&gt;</span> &amp;<span class="type">str</span> &#123;</span><br><span class="line">        <span class="built_in">unimplemented!</span>()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">pub</span> <span class="keyword">fn</span> <span class="title function_">age</span>(&amp;<span class="keyword">self</span>) <span class="punctuation">-&gt;</span> <span class="type">u32</span> &#123;</span><br><span class="line">        <span class="built_in">unimplemented!</span>()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">pub</span> <span class="keyword">fn</span> <span class="title function_">weight</span>(&amp;<span class="keyword">self</span>) <span class="punctuation">-&gt;</span> <span class="type">f32</span> &#123;</span><br><span class="line">        <span class="built_in">unimplemented!</span>()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">pub</span> <span class="keyword">fn</span> <span class="title function_">set_age</span>(&amp;<span class="keyword">mut</span> <span class="keyword">self</span>, new_age: <span class="type">u32</span>) &#123;</span><br><span class="line">        <span class="built_in">unimplemented!</span>()</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">pub</span> <span class="keyword">fn</span> <span class="title function_">set_weight</span>(&amp;<span class="keyword">mut</span> <span class="keyword">self</span>, new_weight: <span class="type">f32</span>) &#123;</span><br><span class="line">        <span class="built_in">unimplemented!</span>()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">bob</span> = User::<span class="title function_ invoke__">new</span>(<span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;Bob&quot;</span>), <span class="number">32</span>, <span class="number">155.2</span>);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;I&#x27;m &#123;&#125; and my age is &#123;&#125;&quot;</span>, bob.<span class="title function_ invoke__">name</span>(), bob.<span class="title function_ invoke__">age</span>());</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#[test]</span></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">test_weight</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">bob</span> = User::<span class="title function_ invoke__">new</span>(<span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;Bob&quot;</span>), <span class="number">32</span>, <span class="number">155.2</span>);</span><br><span class="line">    <span class="built_in">assert_eq!</span>(bob.<span class="title function_ invoke__">weight</span>(), <span class="number">155.2</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#[test]</span></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">test_set_age</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">bob</span> = User::<span class="title function_ invoke__">new</span>(<span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;Bob&quot;</span>), <span class="number">32</span>, <span class="number">155.2</span>);</span><br><span class="line">    <span class="built_in">assert_eq!</span>(bob.<span class="title function_ invoke__">age</span>(), <span class="number">32</span>);</span><br><span class="line">    bob.<span class="title function_ invoke__">set_age</span>(<span class="number">33</span>);</span><br><span class="line">    <span class="built_in">assert_eq!</span>(bob.<span class="title function_ invoke__">age</span>(), <span class="number">33</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我的答案：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">User</span> &#123;</span><br><span class="line">    name: <span class="type">String</span>,</span><br><span class="line">    age: <span class="type">u32</span>,</span><br><span class="line">    weight: <span class="type">f32</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">User</span> &#123;</span><br><span class="line">    <span class="keyword">pub</span> <span class="keyword">fn</span> <span class="title function_">new</span>(name: <span class="type">String</span>, age: <span class="type">u32</span>, weight: <span class="type">f32</span>) <span class="punctuation">-&gt;</span> <span class="keyword">Self</span> &#123;</span><br><span class="line">        User &#123;</span><br><span class="line">            name,</span><br><span class="line">            age,</span><br><span class="line">            weight,</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">pub</span> <span class="keyword">fn</span> <span class="title function_">name</span>(&amp;<span class="keyword">self</span>) <span class="punctuation">-&gt;</span> &amp;<span class="type">str</span> &#123;</span><br><span class="line">        &amp;<span class="keyword">self</span>.name</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">pub</span> <span class="keyword">fn</span> <span class="title function_">age</span>(&amp;<span class="keyword">self</span>) <span class="punctuation">-&gt;</span> <span class="type">u32</span> &#123;</span><br><span class="line">        <span class="keyword">self</span>.age</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">pub</span> <span class="keyword">fn</span> <span class="title function_">weight</span>(&amp;<span class="keyword">self</span>) <span class="punctuation">-&gt;</span> <span class="type">f32</span> &#123;</span><br><span class="line">        <span class="keyword">self</span>.weight</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">pub</span> <span class="keyword">fn</span> <span class="title function_">set_age</span>(&amp;<span class="keyword">mut</span> <span class="keyword">self</span>, new_age: <span class="type">u32</span>) &#123;</span><br><span class="line">        <span class="keyword">self</span>.age = new_age</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">pub</span> <span class="keyword">fn</span> <span class="title function_">set_weight</span>(&amp;<span class="keyword">mut</span> <span class="keyword">self</span>, new_weight: <span class="type">f32</span>) &#123;</span><br><span class="line">        <span class="keyword">self</span>.weight = new_weight</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">bob</span> = User::<span class="title function_ invoke__">new</span>(<span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;Bob&quot;</span>), <span class="number">32</span>, <span class="number">155.2</span>);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;I&#x27;m &#123;&#125; and my age is &#123;&#125;, weight is &#123;&#125;&quot;</span>, bob.<span class="title function_ invoke__">name</span>(), bob.<span class="title function_ invoke__">age</span>(), bob.<span class="title function_ invoke__">weight</span>());</span><br><span class="line">    bob.<span class="title function_ invoke__">set_age</span>(<span class="number">16</span>);</span><br><span class="line">    bob.<span class="title function_ invoke__">set_weight</span>(<span class="number">101</span> <span class="keyword">as</span> <span class="type">f32</span>);</span><br><span class="line">    <span class="built_in">println!</span>(<span class="string">&quot;I&#x27;m &#123;&#125; and my age is &#123;&#125;, weight is &#123;&#125;&quot;</span>, bob.<span class="title function_ invoke__">name</span>(), bob.<span class="title function_ invoke__">age</span>(), bob.<span class="title function_ invoke__">weight</span>());</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#[test]</span></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">test_weight</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="variable">bob</span> = User::<span class="title function_ invoke__">new</span>(<span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;Bob&quot;</span>), <span class="number">32</span>, <span class="number">155.2</span>);</span><br><span class="line">    <span class="built_in">assert_eq!</span>(bob.<span class="title function_ invoke__">weight</span>(), <span class="number">155.2</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#[test]</span></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">test_set_age</span>() &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">bob</span> = User::<span class="title function_ invoke__">new</span>(<span class="type">String</span>::<span class="title function_ invoke__">from</span>(<span class="string">&quot;Bob&quot;</span>), <span class="number">32</span>, <span class="number">155.2</span>);</span><br><span class="line">    <span class="built_in">assert_eq!</span>(bob.<span class="title function_ invoke__">age</span>(), <span class="number">32</span>);</span><br><span class="line">    bob.<span class="title function_ invoke__">set_age</span>(<span class="number">33</span>);</span><br><span class="line">    <span class="built_in">assert_eq!</span>(bob.<span class="title function_ invoke__">age</span>(), <span class="number">33</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="多边形结构"><a href="#多边形结构" class="headerlink" title="多边形结构"></a>多边形结构</h3><p>我们将创建一个多边形结构，其中包含一些点：</p>
<figure class="highlight rust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// <span class="doctag">TODO:</span> remove this when you&#x27;re done with your implementation.</span></span><br><span class="line"><span class="meta">#![allow(unused_variables, dead_code)]</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">pub</span> <span class="keyword">struct</span> <span class="title class_">Point</span> &#123;</span><br><span class="line">    <span class="comment">// add fields</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">Point</span> &#123;</span><br><span class="line">    <span class="comment">// add methods</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">pub</span> <span class="keyword">struct</span> <span class="title class_">Polygon</span> &#123;</span><br><span class="line">    <span class="comment">// add fields</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">Polygon</span> &#123;</span><br><span class="line">    <span class="comment">// add methods</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">pub</span> <span class="keyword">struct</span> <span class="title class_">Circle</span> &#123;</span><br><span class="line">    <span class="comment">// add fields</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">impl</span> <span class="title class_">Circle</span> &#123;</span><br><span class="line">    <span class="comment">// add methods</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">pub</span> <span class="keyword">enum</span> <span class="title class_">Shape</span> &#123;</span><br><span class="line">    <span class="title function_ invoke__">Polygon</span>(Polygon),</span><br><span class="line">    <span class="title function_ invoke__">Circle</span>(Circle),</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#[cfg(test)]</span></span><br><span class="line"><span class="keyword">mod</span> tests &#123;</span><br><span class="line">    <span class="keyword">use</span> super::*;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">round_two_digits</span>(x: <span class="type">f64</span>) <span class="punctuation">-&gt;</span> <span class="type">f64</span> &#123;</span><br><span class="line">        (x * <span class="number">100.0</span>).<span class="title function_ invoke__">round</span>() / <span class="number">100.0</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">#[test]</span></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">test_point_magnitude</span>() &#123;</span><br><span class="line">        <span class="keyword">let</span> <span class="variable">p1</span> = Point::<span class="title function_ invoke__">new</span>(<span class="number">12</span>, <span class="number">13</span>);</span><br><span class="line">        <span class="built_in">assert_eq!</span>(<span class="title function_ invoke__">round_two_digits</span>(p1.<span class="title function_ invoke__">magnitude</span>()), <span class="number">17.69</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">#[test]</span></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">test_point_dist</span>() &#123;</span><br><span class="line">        <span class="keyword">let</span> <span class="variable">p1</span> = Point::<span class="title function_ invoke__">new</span>(<span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">        <span class="keyword">let</span> <span class="variable">p2</span> = Point::<span class="title function_ invoke__">new</span>(<span class="number">14</span>, <span class="number">13</span>);</span><br><span class="line">        <span class="built_in">assert_eq!</span>(<span class="title function_ invoke__">round_two_digits</span>(p1.<span class="title function_ invoke__">dist</span>(p2)), <span class="number">5.00</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">#[test]</span></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">test_point_add</span>() &#123;</span><br><span class="line">        <span class="keyword">let</span> <span class="variable">p1</span> = Point::<span class="title function_ invoke__">new</span>(<span class="number">16</span>, <span class="number">16</span>);</span><br><span class="line">        <span class="keyword">let</span> <span class="variable">p2</span> = p1 + Point::<span class="title function_ invoke__">new</span>(-<span class="number">4</span>, <span class="number">3</span>);</span><br><span class="line">        <span class="built_in">assert_eq!</span>(p2, Point::<span class="title function_ invoke__">new</span>(<span class="number">12</span>, <span class="number">19</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">#[test]</span></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">test_polygon_left_most_point</span>() &#123;</span><br><span class="line">        <span class="keyword">let</span> <span class="variable">p1</span> = Point::<span class="title function_ invoke__">new</span>(<span class="number">12</span>, <span class="number">13</span>);</span><br><span class="line">        <span class="keyword">let</span> <span class="variable">p2</span> = Point::<span class="title function_ invoke__">new</span>(<span class="number">16</span>, <span class="number">16</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">poly</span> = Polygon::<span class="title function_ invoke__">new</span>();</span><br><span class="line">        poly.<span class="title function_ invoke__">add_point</span>(p1);</span><br><span class="line">        poly.<span class="title function_ invoke__">add_point</span>(p2);</span><br><span class="line">        <span class="built_in">assert_eq!</span>(poly.<span class="title function_ invoke__">left_most_point</span>(), <span class="title function_ invoke__">Some</span>(p1));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">#[test]</span></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">test_polygon_iter</span>() &#123;</span><br><span class="line">        <span class="keyword">let</span> <span class="variable">p1</span> = Point::<span class="title function_ invoke__">new</span>(<span class="number">12</span>, <span class="number">13</span>);</span><br><span class="line">        <span class="keyword">let</span> <span class="variable">p2</span> = Point::<span class="title function_ invoke__">new</span>(<span class="number">16</span>, <span class="number">16</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">poly</span> = Polygon::<span class="title function_ invoke__">new</span>();</span><br><span class="line">        poly.<span class="title function_ invoke__">add_point</span>(p1);</span><br><span class="line">        poly.<span class="title function_ invoke__">add_point</span>(p2);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">let</span> <span class="variable">points</span> = poly.<span class="title function_ invoke__">iter</span>().<span class="title function_ invoke__">cloned</span>().collect::&lt;<span class="type">Vec</span>&lt;_&gt;&gt;();</span><br><span class="line">        <span class="built_in">assert_eq!</span>(points, <span class="built_in">vec!</span>[Point::<span class="title function_ invoke__">new</span>(<span class="number">12</span>, <span class="number">13</span>), Point::<span class="title function_ invoke__">new</span>(<span class="number">16</span>, <span class="number">16</span>)]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">#[test]</span></span><br><span class="line">    <span class="keyword">fn</span> <span class="title function_">test_shape_perimeters</span>() &#123;</span><br><span class="line">        <span class="keyword">let</span> <span class="keyword">mut </span><span class="variable">poly</span> = Polygon::<span class="title function_ invoke__">new</span>();</span><br><span class="line">        poly.<span class="title function_ invoke__">add_point</span>(Point::<span class="title function_ invoke__">new</span>(<span class="number">12</span>, <span class="number">13</span>));</span><br><span class="line">        poly.<span class="title function_ invoke__">add_point</span>(Point::<span class="title function_ invoke__">new</span>(<span class="number">17</span>, <span class="number">11</span>));</span><br><span class="line">        poly.<span class="title function_ invoke__">add_point</span>(Point::<span class="title function_ invoke__">new</span>(<span class="number">16</span>, <span class="number">16</span>));</span><br><span class="line">        <span class="keyword">let</span> <span class="variable">shapes</span> = <span class="built_in">vec!</span>[</span><br><span class="line">            Shape::<span class="title function_ invoke__">from</span>(poly),</span><br><span class="line">            Shape::<span class="title function_ invoke__">from</span>(Circle::<span class="title function_ invoke__">new</span>(Point::<span class="title function_ invoke__">new</span>(<span class="number">10</span>, <span class="number">20</span>), <span class="number">5</span>)),</span><br><span class="line">        ];</span><br><span class="line">        <span class="keyword">let</span> <span class="variable">perimeters</span> = shapes</span><br><span class="line">            .<span class="title function_ invoke__">iter</span>()</span><br><span class="line">            .<span class="title function_ invoke__">map</span>(Shape::perimeter)</span><br><span class="line">            .<span class="title function_ invoke__">map</span>(round_two_digits)</span><br><span class="line">            .collect::&lt;<span class="type">Vec</span>&lt;_&gt;&gt;();</span><br><span class="line">        <span class="built_in">assert_eq!</span>(perimeters, <span class="built_in">vec!</span>[<span class="number">15.48</span>, <span class="number">31.42</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#[allow(dead_code)]</span></span><br><span class="line"><span class="keyword">fn</span> <span class="title function_">main</span>() &#123;&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Wyatex
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://wyatex.online/Rust/rust-note-3-google/" title="rust学习笔记（3）：Day2 Moring">https://wyatex.online/Rust/rust-note-3-google/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="tag"># 学习笔记</a>
              <a href="/tags/Rust/" rel="tag"># Rust</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/%E8%AF%AD%E8%A8%80%E5%A4%A7%E6%9D%82%E7%83%A9%EF%BC%881%EF%BC%89/" rel="prev" title="语言大杂烩（1）">
                  <i class="fa fa-angle-left"></i> 语言大杂烩（1）
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/ChatGpt/chatgpt-guide/" rel="next" title="chatGPT（VX版）使用说明书">
                  chatGPT（VX版）使用说明书 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2019 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Wyatex</span>
  </div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/ribbon.js/dist/ribbon.min.js"></script>
  <script src="/lib/animejs/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="/lib/@next-theme/pjax/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="/lib/@fancyapps/ui/dist/fancybox/fancybox.umd.js" integrity="sha256-q8XkJ6dj5VwSvzI8+nATCHHQG+Xv/dAZBCgqmu93zOY=" crossorigin="anonymous"></script>
  <script src="/lib/lozad/dist/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="/lib/hexo-generator-searchdb/dist/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
